package crypto

import (
	"fmt"
	"strings"
)

//Hash 使用默认算法计算Hash
func Hash(data []byte) ([]byte, error) {
	return HashWithOpts(data, options.HashFamily)
}

//HashWithOpts 使用指定算法计算Hash
func HashWithOpts(data []byte, algorithm string) ([]byte, error) {
	algorithm = strings.ToUpper(algorithm)
	hasher, exists := csp.Hashers[algorithm]
	if exists {
		h := hasher.New()
		h.Write(data)
		return h.Sum(nil), nil
	}
	return nil, fmt.Errorf("hash algorithm not exist: %s", algorithm)
}

// HashSize 获取哈希长度，单位字节
func HashSize() (int, error) {
	hasher, exists := csp.Hashers[options.HashFamily]
	if exists {
		return hasher.Size(), nil
	}
	return 0, fmt.Errorf("not existed hash algorithm: %s", options.HashFamily)
}
